package club.youth996.controller;


import club.youth996.entity.MallGoods;
import club.youth996.entity.MallTab;
import club.youth996.service.MallGoodsService;
import club.youth996.service.MallTabService;
import club.youth996.util.Constant;
import club.youth996.util.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zhan Xinjian
 * @since 2020-07-08
 */
@CrossOrigin
@RestController
@RequestMapping("/mallGoods")
public class MallGoodsController {
    @Autowired
    MallGoodsService mallGoodsService;
    @Autowired
    MallTabService mallTabService;

    @RequestMapping("/getRecommend")
    public R getRecommend() {
        List<MallGoods>  mallGoods= mallGoodsService.list(new QueryWrapper<MallGoods>().eq("is_features", Constant.WHETHERRECOMMENDED));
        if (mallGoods != null) {
            HashMap<String, Object> map = new HashMap<>(128);
            map.put("data", mallGoods);
            return R.ok(map);
        }else {
            return R.error();
        }
    }

    /**
     * 根据商品类型分页查询商品信息
     * @param type
     * @param page
     * @return
     */
    @GetMapping("/getGoodData")
    public R getGoodData(String type, int page)  {
        MallTab mallTab = mallTabService.getOne(new QueryWrapper<MallTab>().eq("tab_name", type));
        if (mallTab != null) {
            Page<MallGoods>  mallGoodsPage= new Page<MallGoods>(page, Constant.NUMBER_OF_PGES_DISPLAYED);
            IPage<MallGoods> mallGoodsIPage = mallGoodsService.page(mallGoodsPage, new QueryWrapper<MallGoods>().eq("tab_id", mallTab.getTabId()));
            if (mallGoodsIPage != null) {
                HashMap<String, Object> map = new HashMap<>(128);
                map.put("data", mallGoodsIPage);
                return R.ok(map);
            }else {
                return R.error();
            }
        }else{
            return R.error();
        }
    }

}

